﻿#region License

// Another free, open solution by someone who gets it.
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Collections.Generic;
using System.Linq;

using AutoMapper;

using Patterns.Validation;
using Patterns.Validation.Advanced;

namespace Patterns.Reflection
{
	/// <summary>
	/// 	Encapsulates a lazy-loaded collection of lexical closures that provide access to the setters of reflected member values.
	/// </summary>
	public class MemberValuePersisters<T> : Dictionary<MemberName, Action<object, IEnumerable<object>>> where T : class
	{
		private readonly IMappingEngine _mapper;
		private readonly ReflectedObject<T> _reflection;

		/// <summary>
		/// 	Initializes a new instance of the <see cref = "MemberValuePersisters&lt;T&gt;" /> class.
		/// </summary>
		/// <param name = "reflection">The reflection.</param>
		/// <param name = "mapper">The mapper.</param>
		public MemberValuePersisters(ReflectedObject<T> reflection, IMappingEngine mapper)
		{
//			_lazyCreator = GetMemberValuePersistence;
			_reflection = reflection;
			_mapper = mapper;
		}

		private Action<object, IEnumerable<object>> GetMemberValuePersistence(MemberName name)
		{
			IndexedProperty indexedProperty = _reflection.GetIndexedProperty(name);
			if (indexedProperty != null && indexedProperty.CanSet)
			{
				return (value, args) =>
				{
					value.Should().NotBeNull();
					args.Should().NotBeEmpty();

					Type memberValueType = indexedProperty.MemberValueType;
					Type sourceType = value.GetType();

					object mappedValue = memberValueType.IsAssignableFrom(sourceType)
					                     	? value
					                     	: _mapper.Map(value, sourceType, memberValueType);

					indexedProperty.Set(args.ToArray(), mappedValue);
				};
			}

			FieldOrProperty property = _reflection.GetProperty(name);
			if (property != null && property.CanSet)
			{
				return (value, args) =>
				{
					value.Should().NotBeNull();

					Type memberValueType = property.MemberValueType;
					Type sourceType = value.GetType();

					object mappedValue = memberValueType.IsAssignableFrom(sourceType)
					                     	? value
					                     	: _mapper.Map(value, sourceType, memberValueType);

					property.Set(mappedValue);
				};
			}

			FieldOrProperty field = _reflection.GetField(name);
			if (field != null && field.CanSet)
			{
				return (value, args) =>
				{
					value.Should().NotBeNull();

					Type memberValueType = field.MemberValueType;
					Type sourceType = value.GetType();

					object mappedValue = memberValueType.IsAssignableFrom(sourceType)
					                     	? value
					                     	: _mapper.Map(value, sourceType, memberValueType);

					field.Set(mappedValue);
				};
			}

			return null;
		}
	}
}